//
//  FindCarViewController.m
//  Cars' home
//
//  Created by dapeng on 15/11/27.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "FindCarViewController.h"
#import "Theme-PrefixHeader.pch"
@interface FindCarViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, findCarDelegate, recomCarDelegate, UISearchBarDelegate>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) SAPFindCarTwoModel *twoModel;
@property (nonatomic, assign) NSInteger count;
@property (nonatomic, retain) SAPSellCarViewController *sellCarView;
@property (nonatomic, retain) SAPSellDetailsViewController *sellDetailCarView;
@property (nonatomic, retain) UISearchController *searchController;
@property (nonatomic, retain) UISearchBar *searchBar;

@end

@implementation FindCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self searchBars];

    [self collectionViews];
    self.view.backgroundColor = [[ConfigurationTheme shareInstance] getThemeColorWithName:@"ygColor"];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(tapAction:)];
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    _HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    _HUD.color = [UIColor blackColor];
    _HUD.dimBackground = YES;
    _HUD.labelText = @"正在加载";
    [SAPNetWorkTool getWithUrl:recomCar parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
        NSDictionary *dic = [result valueForKey:@"result"];
        self.twoModel = [[SAPFindCarTwoModel alloc] initWithDataSource:dic];
        _HUD.hidden = YES;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
    
}
//改变主题的通知
- (void)changeColor {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"nightModel"];
    
    if (!(passWord == nil) && [passWord isEqualToString:@"day"]) {
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        self.searchBar.backgroundColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        self.tabBarController.tabBar.barTintColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        
    }else{
        self.searchBar.backgroundColor = [UIColor grayColor];

        self.navigationController.navigationBar.barTintColor = [UIColor grayColor];
        self.tabBarController.tabBar.barTintColor = [UIColor whiteColor];
        
    }
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    [self.searchBar resignFirstResponder];
}
- (void)collectionViews {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 49);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[SAPFindCarCollectionViewCell class] forCellWithReuseIdentifier:@"SAPFindCarCollectionViewCellIndentifier"];
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SAPFindCarCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SAPFindCarCollectionViewCellIndentifier" forIndexPath:indexPath];
    cell.finddelegate =self;

    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 1;
}
- (void)passValue:(NSString *)webView {
    self.sellCarView = [[SAPSellCarViewController alloc] init];
    _sellCarView.url = webView;
    [self.navigationController pushViewController:self.sellCarView animated:YES];
    
}
- (void)passValueWeb:(NSString *)url {
    SAPSellDetailsViewController *sellDetailVC = [[SAPSellDetailsViewController alloc] init];
    sellDetailVC.url = url;
    [self.navigationController pushViewController:sellDetailVC animated:YES];
}

- (void)pushToCollectVC {
    SAPCollectViewController *collectVC = [[SAPCollectViewController alloc] init];
    [self.navigationController pushViewController:collectVC animated:YES];

}
- (void)pushAudioPage {
    SAPAudioViewController *audioVC = [[SAPAudioViewController alloc] init];
    [self.navigationController pushViewController:audioVC animated:YES];
}
- (void)pushHistoryPage {
    SAPHistoryViewController *historyVC = [[SAPHistoryViewController alloc] init];
    [self.navigationController pushViewController:historyVC animated:YES];
}
- (void)searchBars {
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, 44)];
    self.searchBar.backgroundColor = [UIColor grayColor];
    self.searchBar.delegate = self;
//    [self.view addSubview:self.searchBar];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.titleView = self.searchBar;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    SAPSearchDetailsViewController *detailVC = [[SAPSearchDetailsViewController alloc] init];
    NSString *string = [self.searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *url = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov5.1.0/sou/series.ashx?app=2&platform=1&version=5.1.0&kw=%@&deviceid=06aa08c16106fcc6bab0b93d1503ee05d9b5263d&issug=0", string];
    detailVC.searchText = self.searchBar.text;
    detailVC.url = url;
    [self.navigationController pushViewController:detailVC animated:NO];
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    [self changeColor];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
